import re

chr = 3
start,end = 20034100,20147100

result = list()
with open("chr.bed","r",encoding="utf8") as f:
    total,mark = 0,0
    while True:
        line = f.readline().strip().split('\t')
        if line == [""]:
            break
        total+=1
        temp = re.findall(f"([cC]hr)?({chr})$",line[0])
        if line[4] != "-" or \
            not temp or \
            int(line[2]) < start or \
            int(line[1]) > end:
            continue
        mark+=1
        line.append("\n")
        line[0] = temp[0][1]
        result.append("\t".join(line))

with open("geneX.bed","a+",encoding="utf8") as f:
    f.write(f"# geneX: {mark}; total: {total}\n")
    f.writelines(result)

